# $Id: PKGBUILD 90100 2013-05-06 19:39:54Z foutrelis $
# Maintainer: Ionut Biru <ibiru@archlinux.org>
# Maintainer: Sébastien Luttringer

pkgbase=virtualbox
pkgname=('virtualbox'
         'virtualbox-host-dkms'
         'virtualbox-guest-dkms'
         'virtualbox-sdk'
         'virtualbox-guest-utils'
         'virtualbox-ext-vnc')
pkgver=4.2.12
pkgrel=2
arch=('i686' 'x86_64')
url='http://virtualbox.org'
license=('GPL' 'custom')
makedepends=('alsa-lib'
             'bin86'
             'cdrkit'
             'curl'
             'dev86'
             'device-mapper'
             'glu'
             'gsoap'
             'iasl'
             'jdk7-openjdk'
             'libidl2'
             'libpulse'
             'libstdc++5'
             'libvncserver'
             'libxcomposite'
             'libxcursor'
             'libxinerama'
             'libxml2'
             'libxmu'
             'libxrandr'
             'libxslt'
             'libxtst'
             'linux-headers'
             'mesa'
             'python2'
             'qt4'
             'sdl'
             'sdl_ttf'
             'vde2'
             'xalan-c'
             'xf86driproto'
             'xorg-server-devel')
[[ $CARCH == "x86_64" ]] && makedepends=("${makedepends[@]}" 'gcc-multilib' 'lib32-glibc')
source=("http://download.virtualbox.org/virtualbox/$pkgver/VirtualBox-$pkgver.tar.bz2"
        '10-vboxdrv.rules'
        '60-vboxguest.rules'
        'LocalConfig.kmk'
        'vboxservice.service'
        'vboxweb.service'
        '001-vboxdrv-reference.patch'
        '002-change_default_driver_dir.patch'
        '003-gcc-48.patch'
        '004-system-xorg.patch')
md5sums=('654e45054ae6589452508d37403dc800'
         '5f85710e0b8606de967716ded7b2d351'
         'ed1341881437455d9735875ddf455fbe'
         '58d6e5bb4b4c1c6f3f0b3daa6aaeed03'
         '07c5f6d86c4b7839d719c8ee0c53653b'
         'bc9efed88e0469cd7fc460d5a5cd7b4b'
         '6dbd16b9d1530fc42ff6904cd80be91d'
         '97e193f050574dd272a38e5ee5ebe62b'
         '7cf8611ae5850b86323b690e430eeed7'
         '4da461f8c90caa123b47c62479f426b4')

prepare() {
    cd "VirtualBox-$pkgver"
    # Apply patches
    for _p in "$srcdir"/*.patch; do
        [[ -e $_p ]] || continue
        msg2 "Appy patch ${_p##*/}"
        patch -Np1 -i "$_p"
    done
}

build() {
    cd "VirtualBox-$pkgver"
    cp "$srcdir/LocalConfig.kmk" .
    # fake makeself binary to compile without nofatal
    ln -s /bin/echo makeself
    export PATH="$CWD:$PATH"

    ./configure --disable-docs \
        --enable-webservice \
        --enable-vde \
        --enable-vnc \
        --disable-kmods
    source ./env.sh
    kmk all

    # fix python2
    sed -i 's_^#!.*/usr/bin/python_#!/usr/bin/python2_' "out/linux.$BUILD_PLATFORM_ARCH/release/bin/vboxshell.py"

    # build rdesktop-vrdp (broken from LocalConfig.kmk)
    pushd src/VBox/RDP/client
    cp "$srcdir/LocalConfig.kmk" .
    # fix keymap path FS#32548
    sed -i 's:/opt/VirtualBox:/usr/share/virtualbox:' Makefile.kmk
    kmk all
    popd

    # build VNC pack
    pushd src/VBox/ExtPacks/VNC
    kmk packing
    popd
}

package_virtualbox() {
    pkgdesc='Powerful x86 virtualization for enterprise as well as home use'
    depends=('curl'
             'libpng'
             'libxcursor'
             'libxinerama'
             'libxml2'
             'libxmu'
             'sdl'
             'shared-mime-info'
             'virtualbox-host-modules')
    optdepends=('qt4: VirtualBox GUI support'
                'vde2: Virtual Distributed Ethernet support'
                'virtualbox-sdk: Developer kit'
                'virtualbox-host-dkms: Host kernel source modules for non-stock kernels'
                'net-tools: Host-only or bridged networking support')
    backup=('etc/vbox/vbox.cfg')
    replaces=('virtualbox-ose')
    conflicts=('virtualbox-ose')
    install=virtualbox.install

    source "VirtualBox-$pkgver/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin"
    install -dm755 "$pkgdir"/usr/{bin,lib/virtualbox/components,lib/virtualbox/ExtensionPacks,share/virtualbox/nls,share/virtualbox/rdesktop-vrdp-keymaps}

    #Binaries and Wrapper with Launchers
    install -m 0755 VBox.sh "$pkgdir/usr/bin/VBox"

    for i in VBoxHeadless VBoxManage VBoxSDL VirtualBox vboxwebsrv VBoxBalloonCtrl; do
        ln -sf VBox "$pkgdir/usr/bin/$i"
        ln -sf VBox "$pkgdir/usr/bin/${i,,}"
    done
    install -m 0755 VBoxTunctl "$pkgdir/usr/bin"
    install -m 0755 rdesktop-vrdp "$pkgdir/usr/bin"

    #components
    install -m 0755 components/* -t "$pkgdir/usr/lib/virtualbox/components"

    #lib
    install -m 0755 *.so "$pkgdir/usr/lib/virtualbox"
    install -m 0644 *.gc *.r0  VBoxEFI*.fd "$pkgdir/usr/lib/virtualbox"

    #setuid root binaries
    install -m 4755 VBoxSDL VirtualBox VBoxHeadless VBoxNetDHCP VBoxNetAdpCtl -t "$pkgdir/usr/lib/virtualbox"
    #other binaries
    install -m 0755 VBoxManage VBoxSVC VBoxExtPackHelperApp VBoxXPCOMIPCD VBoxTestOGL VBoxBalloonCtrl vboxwebsrv webtest -t "$pkgdir/usr/lib/virtualbox"

    #language
    install -m 0755 nls/*.qm -t "$pkgdir/usr/share/virtualbox/nls"

    #rdesktop keymaps
    install -m 0644 rdesktop-vrdp-keymaps/* "$pkgdir/usr/share/virtualbox/rdesktop-vrdp-keymaps"

    #useless scripts
    install -m 0755 VBoxCreateUSBNode.sh VBoxSysInfo.sh -t "$pkgdir/usr/share/virtualbox"

    #icons
    install -D -m 0644 VBox.png "$pkgdir/usr/share/pixmaps/VBox.png"

    pushd icons
    for i in *; do
        install -d "$pkgdir/usr/share/icons/hicolor/$i/mimetypes"
        cp $i/* "$pkgdir/usr/share/icons/hicolor/$i/mimetypes"
    done
    popd

    #desktop
    install -D -m 0644 virtualbox.desktop "$pkgdir/usr/share/applications/virtualbox.desktop"
    install -D -m 0644 virtualbox.xml "$pkgdir/usr/share/mime/packages/virtualbox.xml"

    #install configuration
    mkdir -p "$pkgdir/etc/vbox"
    echo 'INSTALL_DIR=/usr/lib/virtualbox' > "$pkgdir/etc/vbox/vbox.cfg"

    #udev and licence
    install -D -m 0644 "$srcdir/VirtualBox-${pkgver}/COPYING" \
        "$pkgdir/usr/share/licenses/virtualbox/LICENSE"
    install -D -m 0644 "$srcdir/10-vboxdrv.rules" \
        "$pkgdir/usr/lib/udev/rules.d/10-vboxdrv.rules"

    # install systemd service
    install -D -m644 "$srcdir/vboxweb.service" \
        "$pkgdir/usr/lib/systemd/system/vboxweb.service"
}

package_virtualbox-sdk() {
    pkgdesc='VirtualBox Software Developer Kit (SDK)'
    depends=('python2')

    install -dm755 "$pkgdir/usr/lib/virtualbox"

    source "VirtualBox-$pkgver/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin"

    install -D -m 0755 vboxshell.py "$pkgdir/usr/lib/virtualbox/vboxshell.py"
    #python sdk
    pushd sdk/installer
    VBOX_INSTALL_PATH="/usr/lib/virtualbox" python2 vboxapisetup.py install --root "$pkgdir"
    popd
    rm -rf sdk/installer
    mv sdk "$pkgdir/usr/lib/virtualbox"
}

package_virtualbox-host-dkms() {
    pkgdesc='VirtualBox Host kernel modules sources'
    depends=('dkms' 'gcc' 'make')
    provides=('virtualbox-host-modules')
    replaces=('virtualbox-source' 'virtualbox-host-source')
    conflicts=('virtualbox-source' 'virtualbox-host-source')
    optdepends=('linux-headers'
                'linux-lts-headers')
    install=virtualbox-host-dkms.install

    install -dm755 "$pkgdir/usr/src"
    source "VirtualBox-$pkgver/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin"
    cp -r src "$pkgdir/usr/src/vboxhost-$pkgver"
}

package_virtualbox-guest-dkms() {
    pkgdesc='VirtualBox Guest kernel modules sources'
    depends=('dkms' 'gcc' 'make')
    provides=('virtualbox-guest-modules')
    replaces=('virtualbox-archlinux-source' 'virtualbox-guest-source')
    conflicts=('virtualbox-archlinux-source' 'virtualbox-guest-source')
    optdepends=('linux-headers'
                'linux-lts-headers')
    install=virtualbox-guest-dkms.install

    install -dm755 "$pkgdir/usr/src"
    source "VirtualBox-$pkgver/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions"
    cp -r src "$pkgdir/usr/src/vboxguest-$pkgver"
}

package_virtualbox-guest-utils() {
    pkgdesc='VirtualBox Guest userspace utilities'
    depends=('virtualbox-guest-modules' 'gcc-libs' 'libxmu' 'xorg-xrandr' 'libxfixes')
    optdepends=('virtualbox-guest-dkms: Guest kernel source modules for non-stock kernels')
    replaces=('virtualbox-archlinux-additions' 'virtualbox-guest-additions')
    conflicts=('virtualbox-archlinux-additions' 'virtualbox-guest-additions')
    install=virtualbox-guest-utils.install

    source "VirtualBox-$pkgver/env.sh"
    pushd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions"
    install -d "$pkgdir"/{usr/bin,sbin}
    install -m755 VBoxClient VBoxControl VBoxService "$pkgdir/usr/bin"
    install -m755 mount.vboxsf "$pkgdir/sbin"
    install -m755 -D "$srcdir"/VirtualBox-$pkgver/src/VBox/Additions/x11/Installer/98vboxadd-xclient \
        "$pkgdir"/usr/bin/VBoxClient-all
    install -m755 -D "$srcdir"/VirtualBox-$pkgver/src/VBox/Additions/x11/Installer/vboxclient.desktop \
        "$pkgdir"/etc/xdg/autostart/vboxclient.desktop
    install -D vboxvideo_drv.so \
        "$pkgdir/usr/lib/xorg/modules/drivers/vboxvideo.so"
    install -d "$pkgdir/usr/lib/xorg/modules/dri"
    install -m755 VBoxOGL*.so "$pkgdir/usr/lib"
    ln -s /usr/lib/VBoxOGL.so "$pkgdir/usr/lib/xorg/modules/dri/vboxvideo_dri.so"
    install -m755 -D pam_vbox.so "$pkgdir/usr/lib/security/pam_vbox.so"
    popd
    # install udev rules
    install -D -m 0644 60-vboxguest.rules \
        "$pkgdir/usr/lib/udev/rules.d/60-vboxguest.rules"
    # install systemd service file
    install -D -m644 vboxservice.service \
        "$pkgdir/usr/lib/systemd/system/vboxservice.service"
}

package_virtualbox-ext-vnc() {
    pkgdesc='VirtualBox VNC extension pack'
    depends=('virtualbox' 'libvncserver')
    optdepends=('tigervnc: vnc client')
    install=virtualbox-ext-vnc.install

    source "VirtualBox-$pkgver/env.sh"
    cd "VirtualBox-$pkgver/out/linux.$BUILD_PLATFORM_ARCH/release/packages"
    install -Dm644 VNC-*.vbox-extpack "$pkgdir/usr/share/virtualbox/extensions/VNC-${pkgver}.vbox-extpack"
}

# vim:set ts=4 sw=4 et:
